#include "MainWindow.h"
#include <QApplication>
#include <QSplashScreen>
#include<QThread>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPixmap pixmap(":/new/prefix1/start.jpg");
    QSplashScreen splash(pixmap);
    splash.show();
    a.processEvents();
    QThread::sleep(2);
    MainWindow w;
    w.show();
    splash.finish(&w);
    return a.exec();
}


#include "MainWindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QVBoxLayout>
#include <QLabel>
#include <QScrollArea>
#include <QListWidgetItem>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QFileDialog>
#include <QTime>
#include <QtMath>
#include <QTableWidget>
#include <QHeaderView>
#include <QSpinBox>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QUrl>
#include<QInputDialog>

FlowerSelectionDialog::FlowerSelectionDialog(QWidget* parent)
    : QDialog(parent)
{
    setWindowTitle("选择花种");

    flowerList = new QListWidget(this);
    flowerList->addItem("绣球");
    flowerList->addItem("铃兰");
    flowerList->addItem("蒲公英");

    QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(flowerList);
    layout->addWidget(buttonBox);
    setLayout(layout);
}

QString FlowerSelectionDialog::selectedFlower() const
{
    QListWidgetItem* item = flowerList->currentItem();
    return item ? item->text() : QString();
}

MusicSelectionDialog::MusicSelectionDialog(QWidget* parent)
    : QDialog(parent)
{
    setWindowTitle("选择背景音乐");

    musicList = new QListWidget(this);
    musicList->addItem("默认音乐");
    musicList->addItem("自定义音乐");

    QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(musicList);
    layout->addWidget(buttonBox);
    setLayout(layout);
}

QString MusicSelectionDialog::selectedMusic() const
{
    QListWidgetItem* item = musicList->currentItem();
    return item ? item->text() : QString();
}

PlanInputDialog::PlanInputDialog(QWidget* parent)
    : QDialog(parent)
{
    setWindowTitle("输入计划");
    resize(500, 400);

    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    planTable = new QTableWidget(this);
    planTable->setColumnCount(3);
    planTable->setHorizontalHeaderLabels(QStringList() << "计划名称" << "时间" << "颜色");
    planTable->horizontalHeader()->setStretchLastSection(true);
    planTable->verticalHeader()->setVisible(false);

    addButton = new QPushButton("添加计划", this);
    removeButton = new QPushButton("删除计划", this);

    connect(addButton, &QPushButton::clicked, this, &PlanInputDialog::addPlanItem);
    connect(removeButton, &QPushButton::clicked, this, &PlanInputDialog::removePlanItem);

    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(addButton);
    buttonLayout->addWidget(removeButton);

    QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    addPlanItem();

    mainLayout->addWidget(planTable);
    mainLayout->addLayout(buttonLayout);
    mainLayout->addWidget(buttonBox);
    setLayout(mainLayout);
}

void PlanInputDialog::addPlanItem()
{
    int row = planTable->rowCount();
    planTable->insertRow(row);

    QTableWidgetItem* nameItem = new QTableWidgetItem("计划阶段 " + QString::number(row+1));
    planTable->setItem(row, 0, nameItem);

    QSpinBox* timeSpinBox = new QSpinBox();
    timeSpinBox->setRange(1, 999);
    timeSpinBox->setValue(10);
    planTable->setCellWidget(row, 1, timeSpinBox);

    QPushButton* colorButton = new QPushButton();
    QColor defaultColor = QColor::fromHsv((row * 60) % 360, 255, 200);
    colorButton->setStyleSheet(QString("background-color: %1").arg(defaultColor.name()));
    connect(colorButton, &QPushButton::clicked, [=]()
    {
        QColor color = QColorDialog::getColor(defaultColor, this, "选择颜色");
        if(color.isValid())
        {
            colorButton->setStyleSheet(QString("background-color: %1").arg(color.name()));
        }
    });
    planTable->setCellWidget(row, 2, colorButton);
}

void PlanInputDialog::removePlanItem()
{
    int row = planTable->currentRow();
    if(row >= 0)
    {
        planTable->removeRow(row);
    }
}

QVector<PlanItem> PlanInputDialog::getPlanItems()
{
    QVector<PlanItem> items;
    for(int row = 0; row < planTable->rowCount(); ++row)
    {
        PlanItem item;
        item.name = planTable->item(row, 0)->text();

        QSpinBox* spinBox = qobject_cast<QSpinBox*>(planTable->cellWidget(row, 1));
        item.duration = spinBox->value() * 60;
        item.remaining = item.duration;

        QPushButton* colorButton = qobject_cast<QPushButton*>(planTable->cellWidget(row, 2));
        item.color = QColor(colorButton->styleSheet().section(':', 1).section(';', 0, 0).trimmed());
        items.append(item);
    }
    return items;
}

PieChartWidget::PieChartWidget(const QVector<PlanItem>& items, QWidget* parent)
    : QWidget(parent), items(items)
{
    updateItems(items);
    setMinimumSize(300, 300);
}

void PieChartWidget::updateItems(const QVector<PlanItem>& newItems)
{
    items = newItems;
    int total = 0;
    for (const PlanItem& item : items)
    {
        total += item.duration;
    }
    totalDuration = total;
    update();
}

void PieChartWidget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event)

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    int margin = 20;
    int legendWidth = 150;
    int chartSize = qMin(width() - legendWidth - margin*2, height() - margin*2);
    QRectF pieRect(margin, margin, chartSize, chartSize);   
    double startAngle = 0;
    for (const PlanItem& item : items)
    {
        double angle = (item.duration * 360) / totalDuration;
        double completedAngle = ((item.duration - item.remaining) * 360) / totalDuration;

        QColor completedColor = item.color;
        completedColor.setAlpha(200);
        painter.setPen(Qt::black);
        painter.setBrush(completedColor);
        painter.drawPie(pieRect, startAngle * 16, completedAngle * 16);

        QColor remainingColor = item.color;
        remainingColor.setAlpha(100);
        painter.setBrush(remainingColor);
        painter.drawPie(pieRect, (startAngle + completedAngle) * 16, (angle - completedAngle) * 16);


        if(angle > 15)
        {
            double midAngle = startAngle + angle/2;
            QPointF labelPos = pieRect.center() +
                QPointF(cos(midAngle * M_PI / 180) * pieRect.width()/3,
                       -sin(midAngle * M_PI / 180) * pieRect.height()/3);

            painter.drawText(labelPos, QString("%1%\n(%2/%3分钟)")
                           .arg(qRound((item.duration - item.remaining)*100.0/item.duration))
                           .arg((item.duration - item.remaining)/60)
                           .arg(item.duration/60));
        }

        startAngle += angle;
    }


    int legendX = pieRect.right() + margin;
    int legendY = margin;
    for (const PlanItem& item : items)
    {
        painter.setPen(Qt::black);
        painter.setBrush(item.color);
        painter.drawRect(legendX, legendY, 15, 15);

        painter.drawText(legendX + 20, legendY + 12,
                       QString("%1 (%2/%3)")
                       .arg(item.name)
                       .arg((item.duration - item.remaining)/60)
                       .arg(item.duration/60));

        legendY += 20;
    }


    painter.setPen(Qt::black);
    QFont titleFont = painter.font();
    titleFont.setBold(true);
    painter.setFont(titleFont);
    painter.drawText(pieRect.left(), pieRect.top() , "计划进度 (深色:已完成,浅色:未完成)");
}

GardenItem::GardenItem(const QPixmap& pixmap, QWidget* parent)
    : QLabel(parent), m_scale(1.0)
{
    setPixmap(pixmap);
    setScaledContents(false);
    setFixedSize(pixmap.size());
}

void GardenItem::setScale(qreal scale)
{
    if (scale > 0.1 && scale < 5.0)
    {
        m_scale = scale;
        QPixmap original = *pixmap();
        setPixmap(original.scaled(original.size() * m_scale, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        adjustSize();
    }
}

void GardenItem::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_dragPosition = event->pos();
    }
    QLabel::mousePressEvent(event);
}

void GardenItem::mouseMoveEvent(QMouseEvent* event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        move(mapToParent(event->pos() - m_dragPosition));
    }
    QLabel::mouseMoveEvent(event);
}

void GardenItem::wheelEvent(QWheelEvent* event)
{
    if (event->angleDelta().y() > 0)
    {
        setScale(m_scale + 0.1);
    }
    else
    {
        setScale(m_scale - 0.1);
    }
    event->accept();
}

void GardenItem::resizeFlower(int width, int height)
{
    if (width <= 0 || height <= 0)
    {
        return;
    }
    QPixmap original = *pixmap();

    QPixmap resized = original.scaled
    (
        width,
        height,
        Qt::KeepAspectRatio,
        Qt::SmoothTransformation
    );   
    setPixmap(resized);
    setFixedSize(resized.size());
    setScaledContents(false);
}

void GardenItem::contextMenuEvent(QContextMenuEvent* event)
{
    QMenu menu(this);
    QAction* resizeAction = menu.addAction("调整尺寸");
    QAction* deleteAction = menu.addAction("删除");
    QAction* selectedAction = menu.exec(event->globalPos());
    if (selectedAction == resizeAction)
    {

        bool ok;
        int newWidth = QInputDialog::getInt(this, "调整尺寸", "宽度:", pixmap()->width(), 10, 1000, 10, &ok);
        if (ok)
        {
            int newHeight = QInputDialog::getInt(this, "调整尺寸", "高度:", pixmap()->height(), 10, 1000, 10, &ok);
            if (ok)
            {
                resizeFlower(newWidth, newHeight);
            }
        }
    }
    else if (selectedAction == deleteAction)
    {
        deleteLater();
    }
}
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent),ui(new Ui::MainWindow),currentPieDialog(nullptr),pieChartWidget(nullptr),gardenDialog(nullptr)
{
    ui->setupUi(this);
    this->setAcceptDrops(true);

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &MainWindow::updateCountdown);
    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::startCountdown);
    connect(ui->showpie, &QPushButton::clicked, this, &MainWindow::showCurrentPieChart);

    loadFlowerStages();

    currentFlower = "绣球";
    ui->flower->setPixmap(flowerStages[currentFlower][0]);
    ui->timeEdit->setDisplayFormat("mm:ss");
    ui->timeEdit->setTime(QTime(0, 0));
    connect(ui->save, &QPushButton::clicked, this, &MainWindow::saveToAlbum);
    connect(ui->show, &QPushButton::clicked, this, &MainWindow::showAlbum);
    connect(ui->chooseflower, &QPushButton::clicked, this, &MainWindow::chooseFlower);
    connect(ui->planButton, &QPushButton::clicked, this, &MainWindow::inputPlan);
    connect(ui->choosemusic, &QPushButton::clicked, this, &MainWindow::chooseMusic);
    mediaPlayer = new QMediaPlayer(this);
    playlist = new QMediaPlaylist(this);
    mediaPlayer->setPlaylist(playlist);
    connect(ui->garden, &QPushButton::clicked, this, &MainWindow::openGarden);
}

MainWindow::~MainWindow()
{
    delete ui;
    if(currentPieDialog)
    {
        currentPieDialog->deleteLater();
    }
    if(gardenDialog)
    {
        gardenDialog->deleteLater();
    }
}

void MainWindow::loadFlowerStages()
{
    QVector<QPixmap> xiuqiu;
    xiuqiu.append(QPixmap(":/new/prefix1/FB11.png"));
    xiuqiu.append(QPixmap(":/new/prefix1/FB12.png"));
    xiuqiu.append(QPixmap(":/new/prefix1/FB13.png"));
    flowerStages["绣球"] = xiuqiu;

    QVector<QPixmap> linglan;
    linglan.append(QPixmap(":/new/prefix1/a1.jpg"));
    linglan.append(QPixmap(":/new/prefix1/a2.jpg"));
    linglan.append(QPixmap(":/new/prefix1/a3.jpg"));
    linglan.append(QPixmap(":/new/prefix1/a4.jpg"));
    flowerStages["铃兰"] = linglan;

    QVector<QPixmap> popoding;
    popoding.append(QPixmap(":/new/prefix1/b1.jpg"));
    popoding.append(QPixmap(":/new/prefix1/b2.jpg"));
    popoding.append(QPixmap(":/new/prefix1/b3.jpg"));
    popoding.append(QPixmap(":/new/prefix1/b4.jpg"));
    flowerStages["蒲公英"] = popoding;
}

void MainWindow::startCountdown()
{
    if(planItems.isEmpty())
    {
        QMessageBox::warning(this, "警告", "请先输入计划！");
        return;
    }

    totalTime = 0;
    currentStageIndex = 0;
    for(PlanItem& item : planItems)
    {
        totalTime += item.duration;
        item.remaining = item.duration;
    }

    remainingTime = totalTime;
    ui->timeEdit->setTime(QTime(0, remainingTime / 60, remainingTime % 60));
    timer->start(1000);
    ui->pushButton->setEnabled(false);
    ui->planButton->setEnabled(false);
}

void MainWindow::updatePlanProgress(int elapsed)
{
    if(currentStageIndex >= planItems.size())
        return;

    PlanItem& currentItem = planItems[currentStageIndex];
    currentItem.remaining -= elapsed;

    if(currentItem.remaining <= 0)
    {
        elapsed = -currentItem.remaining;
        currentItem.remaining = 0;
        currentStageIndex++;

        if(currentStageIndex < planItems.size())
        {
            timer->stop();
            QMessageBox::information(this, "完成",
                QString("阶段'%1'已完成！请选择新的花种继续。")
                    .arg(planItems[currentStageIndex-1].name));
            chooseFlower();
            timer->start();
        }
    }
}

void MainWindow::updateCountdown()
{
    remainingTime--;

    if (remainingTime >= 0)
    {
        QTime time(0, remainingTime / 60, remainingTime % 60);
        ui->timeEdit->setTime(time);
        updateFlowerImage();
        updatePlanProgress(1);
        if(pieChartWidget)
        {
            pieChartWidget->updateItems(planItems);
        }
    }
    else
    {
        timer->stop();
        showPlanResult();
        QMessageBox::information(this, "完成", "今日计划已完成！");
        ui->pushButton->setEnabled(true);
        ui->planButton->setEnabled(true);
    }
}

void MainWindow::inputPlan()
{
    PlanInputDialog dialog(this);
    if(dialog.exec() == QDialog::Accepted)
    {
        planItems = dialog.getPlanItems();

        int totalMinutes = 0;
        for(const PlanItem& item : planItems)
        {
            totalMinutes += item.duration / 60;
        }
        ui->timeEdit->setTime(QTime(0, totalMinutes, 0));
    }
}

void MainWindow::showCurrentPieChart()
{
    if(planItems.isEmpty())
    {
        QMessageBox::warning(this, "警告", "没有可显示的计划数据！");
        return;
    }

    if(!currentPieDialog)
    {
        currentPieDialog = new QDialog(this);
        currentPieDialog->setWindowTitle("当前计划进度");
        currentPieDialog->resize(500, 450);

        QVBoxLayout* layout = new QVBoxLayout(currentPieDialog);

        pieChartWidget = new PieChartWidget(planItems);
        layout->addWidget(pieChartWidget);

        QPushButton* closeButton = new QPushButton("关闭");
        connect(closeButton, &QPushButton::clicked, currentPieDialog, &QDialog::accept);
        layout->addWidget(closeButton, 0, Qt::AlignRight);

        currentPieDialog->setLayout(layout);
    }

    pieChartWidget->updateItems(planItems);
    currentPieDialog->show();
    currentPieDialog->raise();
    currentPieDialog->activateWindow();
}

void MainWindow::showPlanResult()
{
    QDialog* resultDialog = new QDialog(this);
    resultDialog->setWindowTitle("今日计划完成报告");
    resultDialog->resize(600, 500);
    resultDialog->resize(600, 500);

    QVBoxLayout* layout = new QVBoxLayout(resultDialog);

    QLabel* titleLabel = new QLabel(QString("总用时: %1分钟").arg(totalTime/60));
    titleLabel->setAlignment(Qt::AlignCenter);
    QFont titleFont = titleLabel->font();
    titleFont.setPointSize(14);
    titleLabel->setFont(titleFont);
    layout->addWidget(titleLabel);

    PieChartWidget* resultPieChart = new PieChartWidget(planItems);
    layout->addWidget(resultPieChart);

    QLabel* detailLabel = new QLabel("时间分配详情:");
    layout->addWidget(detailLabel);

    for(const PlanItem& item : planItems)
    {
        QLabel* itemLabel = new QLabel(QString("• %1: %2分钟") .arg(item.name).arg(item.duration/60).arg((item.duration - item.remaining)/60));
        itemLabel->setIndent(20);
        layout->addWidget(itemLabel);
    }

    QPushButton* closeButton = new QPushButton("关闭");
    connect(closeButton, &QPushButton::clicked, resultDialog, &QDialog::accept);
    layout->addWidget(closeButton, 0, Qt::AlignRight);
    resultDialog->setLayout(layout);
    resultDialog->exec();
}

void MainWindow::updateFlowerImage()
{
    int stage = (totalTime - remainingTime) * (flowerStages[currentFlower].size() - 1) / totalTime;
    if (stage >= flowerStages[currentFlower].size())
    {
        stage = flowerStages[currentFlower].size() - 1;
    }
    ui->flower->setPixmap(flowerStages[currentFlower][stage]);
}

void MainWindow::saveToAlbum()
{
    QPixmap currentImage = ui->flower->pixmap()->copy();
    album.append(currentImage);
    QMessageBox::information(this, "成功", "图片已添加到相册!");
}

void MainWindow::showAlbum()
{
    QDialog* albumDialog = new QDialog(this);
    albumDialog->setWindowTitle("花朵留影");
    albumDialog->resize(600, 500);

    QScrollArea* scrollArea = new QScrollArea(albumDialog);
    QWidget* container = new QWidget(scrollArea);
    QVBoxLayout* layout = new QVBoxLayout(container);

    for (const QPixmap& image : album)
    {
        QLabel* imageLabel = new QLabel(container);
        imageLabel->setPixmap(image);
        layout->addWidget(imageLabel);
    }

    container->setLayout(layout);
    scrollArea->setWidget(container);
    scrollArea->setWidgetResizable(true);

    QVBoxLayout* dialogLayout = new QVBoxLayout(albumDialog);
    dialogLayout->addWidget(scrollArea);
    albumDialog->setLayout(dialogLayout);
    albumDialog->exec();
}

void MainWindow::chooseFlower()
{
    FlowerSelectionDialog dialog(this);
    if (dialog.exec() == QDialog::Accepted)
    {
        QString selected = dialog.selectedFlower();
        if (!selected.isEmpty())
        {
            currentFlower = selected;
            ui->flower->setPixmap(flowerStages[currentFlower][0]);
        }
    }
}

void MainWindow::chooseMusic()
{
    MusicSelectionDialog dialog(this);
    if (dialog.exec() == QDialog::Accepted)
    {
        QString selected = dialog.selectedMusic();
        if (!selected.isEmpty())
        {
            if (selected == "默认音乐")
            {
                playlist->clear();
                playlist->addMedia(QUrl(":/new/prefix1/frist music.mp3"));
                mediaPlayer->play();
            }
            else if (selected == "自定义音乐")
            {
                QString fileName = QFileDialog::getOpenFileName(this, "选择音乐文件", "", "音频文件 (*.mp3 *.wav)");
                if (!fileName.isEmpty())
                {
                    playlist->clear();
                    playlist->addMedia(QUrl::fromLocalFile(fileName));
                    mediaPlayer->play();
                }
            }
        }
    }
}
void MainWindow::setupGarden()
{
    gardenDialog = new QDialog(this);
    gardenDialog->setWindowTitle("我的花园");
    gardenDialog->resize(800, 600);

    QVBoxLayout* mainLayout = new QVBoxLayout(gardenDialog);
    QHBoxLayout* controlLayout = new QHBoxLayout();
    QLabel* scaleLabel = new QLabel("缩放大小:", gardenDialog);
    scaleSlider = new QSlider(Qt::Horizontal, gardenDialog);
    scaleSlider->setRange(10, 200);
    scaleSlider->setValue(100);
    scaleSlider->setTickInterval(10);
    connect(scaleSlider, &QSlider::valueChanged, this, &MainWindow::updateScale);

    addFlowerButton = new QPushButton("添加花朵", gardenDialog);
    connect(addFlowerButton, &QPushButton::clicked, this, &MainWindow::addFlowerToGarden);

    QPushButton* clearButton = new QPushButton("清空花园", gardenDialog);
    connect(clearButton, &QPushButton::clicked, [this]()
    {
        for (GardenItem* item : gardenItems)
        {
            item->deleteLater();
        }
        gardenItems.clear();
    });

    controlLayout->addWidget(scaleLabel);
    controlLayout->addWidget(scaleSlider);
    controlLayout->addWidget(addFlowerButton);
    controlLayout->addWidget(clearButton);
    gardenBackground = new QLabel(gardenDialog);
    gardenBackground->setPixmap(QPixmap(":/new/prefix1/garden.jpg").scaled(800, 800, Qt::KeepAspectRatioByExpanding));
    gardenBackground->setScaledContents(true);
    QLabel* hintLabel = new QLabel("右键可缩放大小\n", gardenDialog);
    hintLabel->setStyleSheet("background-color: rgba(255,255,255,150); padding: 5px;");
    hintLabel->setAlignment(Qt::AlignCenter);

    QHBoxLayout* sizeControlLayout = new QHBoxLayout();
    QLabel* sizeLabel = new QLabel("调整尺寸:", gardenDialog);
    QSpinBox* widthSpinBox = new QSpinBox(gardenDialog);
    QSpinBox* heightSpinBox = new QSpinBox(gardenDialog);
    QPushButton* applySizeButton = new QPushButton("应用", gardenDialog);

    widthSpinBox->setRange(10, 1000);
    heightSpinBox->setRange(10, 1000);
    widthSpinBox->setValue(100);
    heightSpinBox->setValue(100);

    sizeControlLayout->addWidget(sizeLabel);
    sizeControlLayout->addWidget(new QLabel("宽度:", gardenDialog));
    sizeControlLayout->addWidget(widthSpinBox);
    sizeControlLayout->addWidget(new QLabel("高度:", gardenDialog));
    sizeControlLayout->addWidget(heightSpinBox);
    sizeControlLayout->addWidget(applySizeButton);

    connect(applySizeButton, &QPushButton::clicked, [=]()
    {
        int newWidth = widthSpinBox->value();
        int newHeight = heightSpinBox->value();
        for (GardenItem* item : gardenItems)
        {
            item->resizeFlower(newWidth, newHeight);
        }
    });

    mainLayout->addLayout(sizeControlLayout);
    mainLayout->addLayout(controlLayout);
    mainLayout->addWidget(gardenBackground);
    mainLayout->addWidget(hintLabel);

    gardenDialog->setLayout(mainLayout);

 }

void MainWindow::addFlowerToGarden()
{
    QString fileName = QFileDialog::getOpenFileName(this, "选择花朵图片", "", "图片文件 (*.png *.jpg *.jpeg)");
    if (!fileName.isEmpty())
    {
        QPixmap pixmap(fileName);
        if (!pixmap.isNull())
        {
            addItemToGarden(pixmap);
        }
    }
}

void MainWindow::addItemToGarden(const QPixmap& pixmap)
{
    GardenItem* item = new GardenItem(pixmap, gardenBackground);
    item->move(gardenBackground->width()/2 - item->width()/2,
               gardenBackground->height()/2 - item->height()/2);
    item->show();

    qreal scale = scaleSlider->value() / 100.0;
    item->setScale(scale);

    gardenItems.append(item);
}

void MainWindow::updateScale(int value)
{
    qreal scale = value / 100.0;
    for (GardenItem* item : gardenItems)
    {
        item->setScale(scale);
    }
}

void MainWindow::openGarden()
{
    if(!gardenDialog)
    {
        setupGarden();
    }
    gardenDialog->show();
    gardenDialog->raise();
    gardenDialog->activateWindow();
}
